package com.lrm.blog.dao;

import com.lrm.blog.po.Blog;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

public interface BlogRepository extends JpaRepository<Blog,Long>, JpaSpecificationExecutor<Blog> {
    @Query("select  b from t_blog b where b.recommend= true")
    List<Blog> findTop(Pageable pageable);

    @Query("select b from t_blog b where b.title like ?1 or b.description like ?1")/*1是指第一个参数就是query，如果要查pageable就是2*/
    Page<Blog> findByQuery(String query,Pageable pageable);

    @Modifying
    @Transactional
    @Query("update t_blog b set b.views=b.views+1 where b.id =?1")
    int updateViews(Long id);

    /*可以直接写原生sql，然后加上nativeQuery = true,而且要改成value="SQL语句的形式"*/

    @Query(value="select date_format(update_time,'%Y') as year from t_blog GROUP BY year ORDER BY  year DESC",nativeQuery = true)
   List<String> findGroupYear();

    @Query(value = "select * from t_blog where date_format(update_time,'%Y') = ?1",nativeQuery = true)
    List<Blog> findByYear(String year);
}
